package com.xiaoyao.novel.crawl;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.parser.BiqiParser;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.HttpUtil;

public class BiqiCrawl extends BaseCrawl {
	private static HttpUtil httpBook = new HttpUtil("GBK");
	private static HttpUtil httpChapter = new HttpUtil("UTF-8");
	private static Logger logger = Logger.getLogger(BiqiCrawl.class);

	public BiqiCrawl() {
		tag = Book.TYPE_ZW69;
	}

	/**
	 * 爬一本书,目录
	 * 
	 * @param webId
	 */
	public void crawlBook(Book book) {
		//http://www.biqi.me/28356/
		String describUrl = "http://www.biqi.me/" + book.webId + "/";
		String page = "";
		try {
			logger.info("读取简介:" + describUrl); // 
			page = httpBook.getPage(describUrl);
			if (ClassUtil.isEmpty(book.describ)) {
				// http://www.5du5.com/book/0/540/简介
				BiqiParser.parseDescrib(page, book);
			}
			//http://www.biqi.me/files/article/html/28/28356/index.html
			String categoryUrl =   book.bookUrl  ;
			logger.info("读取目录:" + categoryUrl);
			page = httpBook.getPage(categoryUrl);
//			logger.debug(page);
			List<Chapter> list = BiqiParser.parseBook(page, book);
			for (int i = book.savedCount; i < list.size(); i++) {
				Chapter chapter = book.list.get(i);
				crawlChapter(chapter);
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}

	/**
	 * 爬章节类容
	 * 
	 * @param chapter
	 * @throws IOException
	 */
	public void crawlChapter(Chapter chapter) throws IOException {
		// http://www.69zw.com/xiaoshuo/38/38997/9006308.html
		String urlStr =  chapter.chapterUrl;
		logger.info("爬取章节 :[" + urlStr + "]");

		String page = "";
		for (int i = 0; i < 3 && ClassUtil.isEmpty(page); i++) {
			try {
				page = httpBook.getPage(urlStr);
			} catch (Exception e) {
				// e.printStackTrace();
				logger.error(e);
			}
		}
		page = filter(page);
		BiqiParser.parseChapter(page, chapter);
		logger.info("内容长度:" + chapter.chapterContent.length());
	}

	// ///////////////////////////////////////////
	// ///////////////////////////////////////////
	/**
	 * 过滤
	 * 
	 * @param page
	 * @return
	 */
	public String filter(String page) {
		page = page.replaceAll("<img.+?>", "");
		page = page.replaceAll("比奇中文网", ""); 
		page = page.replaceAll("比奇", ""); 
		page = page.replaceAll("&.{2,7};", "");
		 page = page.replaceAll("http://www.biqi.me", "");
		 page = page.replaceAll("www.biqi.me", "");
		// 其他替换
		return page;
	}

	public static void main(String[] args) {
		Book book = new Book();
		book.bookId = 63;
		book.webId = "27548"; //
		BiqiCrawl crawl = new BiqiCrawl();
		crawl.crawlBook(book);
	}

}
